package com.business.dynamic.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.business.common.dto.R;
import com.business.dynamic.remote.UserRemote;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisKeysUtil;
import com.business.redis.utils.RedisUtil;
import com.business.user.entity.UserDetailsDto;
import com.business.user.entity.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl {


    @Autowired
    private UserRemote userRemote;

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    /**
     * 获取用户名称列表
     * @param ids
     * @return
     */
    public Map<Integer, UserDetailsDto> getUserNameList(Set<Integer> ids) {
        Map<Integer, UserDetailsDto> result = new HashMap<>();
        //获取用户信息
        JSONArray jsonArray = new JSONArray();
        for (Integer id : ids) {
             String key = String.format(RedisKeyConstant.USER_DETAILS_QUERY_NAME,id);
            if (RedisUtil.hasKey(key)) {
                UserDetailsDto userDto = (UserDetailsDto) RedisUtil.getObject(key, UserDetailsDto.class);
                result.put(userDto.getUserId(), userDto);
            } else {
                jsonArray.add(id);
            }
        }
        if (jsonArray.size() == 0) {
            return result;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ids", jsonArray);
        //调用服务 获取结果
        R<List<UserDetailsDto>> r = userRemote.queryNameList(jsonObject);
        if (r.getCode() == 0) {
            List<UserDetailsDto> list = r.getData();
            for (UserDetailsDto userDto : list) {
                if (userDto != null) {
                    result.put(userDto.getUserId(), userDto);
                }
            }
        }
        return result;
    }

    /**
     * 获取用户名称 和 头像信息
     * @param id
     * @return
     */
    public UserDetailsDto getUserName(Integer id) {
        UserDetailsDto userDto = null;
        String key = String.format(RedisKeyConstant.USER_DETAILS_QUERY_NAME,id);
        JSONObject jsonObject = new JSONObject();
        if (RedisUtil.hasKey(key)) {
            userDto = (UserDetailsDto) RedisUtil.getObject(key, UserDetailsDto.class);
            return userDto;
        } else {
            jsonObject.put("id", id);
        }
        R<UserDetailsDto> r = userRemote.queryName(jsonObject);
        if (r.getCode() == 0) {
            return r.getData();
        } else {
            userDto = new UserDetailsDto();
            userDto.setRealName("未设置");
            return userDto;
        }
    }

    /**
     * 获取用户详情
     * @param id
     * @return
     */
    public UserDetailsDto getUserInfoById(Integer id) {
        UserDetailsDto userDto = null;
        String key = String.format(RedisKeyConstant.USER_DETAILS_QUERY_USER,id);
        JSONObject jsonObject = new JSONObject();
        if (RedisUtil.hasKey(key)) {
            userDto = (UserDetailsDto) RedisUtil.getObject(key, UserDetailsDto.class);
            return userDto;
        } else {
            jsonObject.put("id", id);
        }
        R<UserDetailsDto> r = userRemote.getUserInfoById(jsonObject);
        if (r.getCode() == 0) {
            return r.getData();
        } else {
            userDto = new UserDetailsDto();
            userDto.setRealName("未设置");
            return userDto;
        }
    }
    /**
     * 获取用户的标签列表
     * @param id
     * @return
     */
    public List<String> getTagList(Integer id) {
        String key = String.format(RedisKeyConstant.USER_DETAILS_QUERY_TAG_LIST,id);
        List<String> tagList = null;
        if (RedisUtil.hasKey(key)) {
            tagList = RedisUtil.getArray(key, String.class);
            return tagList;
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", id);
            R<List<String>> r = userRemote.queryTagList(jsonObject);
            if (r.getCode() == 0) {
                return r.getData();
            } else {
                logger.error(r.getMsg());
                return new ArrayList<>();
            }
        }

    }

    /**
     * 获取关注的用户id列表
     * @param id
     * @return
     */
    public Set<Integer> getFriendList(Integer id) {
        String key = String.format(RedisKeyConstant.USER_FRIEND_LIST,id);
        Set<Integer> idList = null;
        if (RedisUtil.hasKey(key)) {
            idList = RedisUtil.getSetValues(key);
            return idList;
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", id);
            R<Set<Integer>> r = userRemote.queryFriendList(jsonObject);
            if (r.getCode() == 0) {
                return r.getData();
            } else {
                logger.error(r.getMsg());
                return new HashSet<>();
            }
        }

    }

}
